package control;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import entity.EventLog;
import entity.Card.*;

/**
 * The Class CardController.
 */
public class CardController {
	
	/**
	 * Creates the card.
	 *
	 * @param isChinese the is chinese
	 */
	public void createCard(boolean isChinese){
		  ReadFile sayF,mcF,unF,eF;
          
          if(isChinese){
                  sayF = new ReadFile("/CardInfo/say_chi.txt");
                  mcF = new ReadFile("/CardInfo/mc_chi.txt");
                  unF=new ReadFile("/CardInfo/under_std_chi.txt");
                  eF=new ReadFile("/CardInfo/event_chi.txt");
          }
          else{
                  sayF = new ReadFile("/CardInfo/say_eng.txt");
                  mcF = new ReadFile("/CardInfo/mc_eng.txt");
                  unF=new ReadFile("/CardInfo/under_std_eng.txt");
                  eF=new ReadFile("/CardInfo/event_eng.txt");
          }
          
          sayF.readNormalCardFile();
          mcF.readMCFile();
          unF.readNormalCardFile();
          eF.readEventCardFile();
          
          for(int i=0;i<sayF.getResult().size(); i++){
                  new SayAndAction(sayF.getResult().get(i),sayF.getEffect().get(i),isChinese);
          }
          
          for(int i=0;i<unF.getResult().size(); i++){
                  new Understanding(unF.getResult().get(i),unF.getEffect().get(i),isChinese);
          }
          for(int i=0;i<eF.getResult().size();i++){
                  new Situation(eF.getResult().get(i),0,isChinese);
          }
          this.createMCcard(mcF,isChinese);
	}
	
	/**
	 * Separate mc effect.
	 *
	 * @param f the f
	 * @return the array list
	 */
	public ArrayList<int[]> separateMCEffect(ReadFile f){
		ArrayList<int[]> markList = new ArrayList<int[]>();
		int[] effect = new int[4];
		int countQ = 0;
        int countM = 0;
        for(int i=0; i<f.getEffect().size();i++){
                        if(i != 0 && i%4==0) {
                                        countQ++;
                                        //System.out.println();
                        }
                        effect[countM] = f.getEffect().get(i);                   
                        countM++;
                        if(countM == 4){
                        		markList.add(effect);
                        		countM =0;  
                        }                
        }
        return markList;
	}
	
	/**
	 * Creates the m ccard.
	 *
	 * @param f the f
	 * @param isChinese the is chinese
	 */
	public void createMCcard(ReadFile f, boolean isChinese){
		ArrayList<int[]> effect2D = new ArrayList<int[]>();
		String[] choice= new String[4];
		effect2D = separateMCEffect(f);
		int count=0;
		for(int i=0; i<effect2D.size();i++){
			for(int j=1;j<5;j++){
				choice[count++]=f.getResult().get(i*5+j);
			}
			count =0;
			new MCcard(f.getResult().get(i*5),choice,effect2D.get(i),isChinese);
		}
	}
	
	/**
	 * Draw card.
	 *
	 * @param boxType the box type
	 * @return the int
	 */
	public int drawCard(char boxType, EventLog event) {		
		Card cardType;
		int effect=0;
		if(boxType == 'S'){
			cardType = new SayAndAction();
			effect = displayContent(cardType,event);
		}
		else if(boxType == 'E'){
			Random rand = new Random();
			int i = rand.nextInt(2)+1;
			if(i == 1){
				cardType = new MCcard();
				effect = displayContent(cardType,event);
			}
			else{
				cardType = new Situation();
				effect = displayContent(cardType,event);
			}
		}
		else if(boxType == 'U'){
			cardType = new Understanding();
			effect = displayContent(cardType,event);
		}
		return effect;
	}
	
	/**
	 * Display content.
	 *
	 * @param c the c
	 * @return the int
	 */
	public int displayContent(Card c,EventLog event){
		int index;
		Vector<Card> card = c.getAllCard();
		index = c.getIndex();
		int effect = card.get(index).display();
		event.setCard(card.get(index));
		event.setChoice(effect);
		return effect;
	}
	
}
